﻿using AutoMapper;
using MediatR;
using Mysqlx.Crud;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.DemandPlanning;
using THE_WIND_RISES.Common.Domans.Produce.ManifestInfos;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.MaterialManagement.Add
{
    public class MaterialPlanningCreateHandler : IRequestHandler<MaterialPlanningCreateCommon, Result>
    {
        private readonly IBaseRepository<MaterialPlanning> MaterialPlanningRepository;
        public IMapper mapper { get; set; }
        public MaterialPlanningCreateHandler(IBaseRepository<MaterialPlanning> materialPlanningRepository, IMapper mapper)
        {
            MaterialPlanningRepository = materialPlanningRepository;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(MaterialPlanningCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };
            var AddData = mapper.Map<MaterialPlanning>(request);
            string sql = "insert into MaterialPlanning values(@MaterialPlanningId,@PlanningCode,@PlanningName,@PlanningDesc,@PlanningState,@PlanningPeole,@DepartmentId,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete);";
            var AddNum = MaterialPlanningRepository.Add(sql, new
            {
                AddData.MaterialPlanningId,
                AddData.PlanningCode,
                AddData.PlanningName,
                AddData.PlanningDesc,
                AddData.PlanningState,
                AddData.PlanningPeole,
                AddData.DepartmentId,
                AddData.Handle,
                AddData.HandleContext,
                AddData.HandleTime,
                AddData.Pid,
                AddData.IsDelete
            });
            if (AddNum > 0)
            {
                result.Code = (int)CommonErrorCodes.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
